<template>
  <div class="home">
    <el-row display="margin-top:10px">
      <el-input v-model="input" placeholder="璇疯緭鍏ヤ功鍚?" style="display:inline-table; width: 30%; float:left"></el-input>
      <el-button type="primary" @click="addBook()" style="float:left; margin: 2px;">鏂板</el-button>
    </el-row>
    <el-row>
      <el-table :data="bookList" style="width: 100%" border>
        <el-table-column prop="id" label="缂栧彿" min-width="100">
          <template scope="scope"> {{ scope.row.pk }} </template>
        </el-table-column>
        <el-table-column prop="book_name" label="涔﹀悕" min-width="100">
          <template scope="scope"> {{ scope.row.fields.book_name }} </template>
        </el-table-column>
        <el-table-column prop="add_time" label="你是不会" min-width="100">
          <template scope="scope"> {{ scope.row.fields.add_time }} </template>
        </el-table-column>
      </el-table>
    </el-row>
  </div>
</template>

<script>
  export default {
    name: 'home',
    data() {
      return {
        input: '',
        bookList: [],
      }
    },
    mounted: function () {
      this.showBooks()
    },
    methods: {
      addBook() {
        this.$http.post('/api/add_book', {parms: this.input})
          .then((response) => {
            var res = JSON.parse(response.bodyText)
            if (res.error_num == 0) {
              this.showBooks()
            } else {
              this.$message.error('鏂板涔︾睄澶辫触锛岃閲嶈瘯')
              console.log(res['msg'])
            }
          })
      },
      showBooks() {
        this.$http.get('/api/show_books')
          .then((response) => {
            var res = JSON.parse(response.bodyText)
            console.log(res)
            if (res.error_num == 0) {
              this.bookList = res['list']
            } else {
              this.$message.error('鏌ヨ涔︾睄澶辫触')
              console.log(res['msg'])
            }
          })
      }
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
  h1, h2 {
    font-weight: normal;
  }

  ul {
    list-style-type: none;
    padding: 0;
  }

  li {
    display: inline-block;
    margin: 0 10px;
  }

  a {
    color: #42b983;
  }
</style>
